﻿using System;
using Microsoft.Win32;

namespace SQLDataTransferer.Common.Utils
{
    public class RegistryUtils
    {
        #region Methods

        public static string[] GetRegistryKeyValuesFromLocalMachine(string registryKey, string registryValue)
        {
            object value = GetRegistryValue(registryKey, registryValue);
            if (value == null)
                return null;

            Type type = value.GetType();

            if (type == typeof(System.String[]))
                return value as string[];

            if (type == typeof(System.String))
                return new string[] { value as string };

            return null;
        }

        public static string GetRegistryKeyValueFromLocalMachine(string registryKey, string registryValue)
        {
            object value = GetRegistryValue(registryKey, registryValue);
            if (value == null)
                return null;

            Type type = value.GetType();

            if (type == typeof(System.String))
                return value as string;

            return null;
        }

        #endregion

        #region Private Helpers

        private static object GetRegistryValue(string registryKey, string registryValue)
        {
            //if (string.IsNullOrEmpty(registryKey))
            //    throw new ArgumentNullException("registryKey");

            //return Registry.GetValue(System.IO.Path.Combine(Registry.LocalMachine.Name, registryKey), registryValue, null);

           var reg = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine,
                                                             Microsoft.Win32.RegistryView.Registry64);

           return reg.OpenSubKey(registryKey).GetValue(registryValue);
        }

        #endregion
    }
}
